package com.h.extensions.listener;

import com.h.extensions.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component
@RabbitListener(queues = Constants.ACK_QUEUE)
public class AckListener {

    @RabbitHandler
    public void handMessage(Message message,Channel channel) throws Exception {
        // 对 Spring AMQP 中的 manual 消息接收操作类型的测试
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            // 消费者逻辑
            System.out.printf("接收到消息: %s, deliveryTag: %d \n", new String(message.getBody(),"UTF-8"),
                    deliveryTag);

            // 进行业务逻辑处理
            System.out.println("业务逻辑处理");
//          int sum = 3/0;
            System.out.println("业务处理完成");
            // 肯定确认
            channel.basicAck(deliveryTag, false);
        }catch (Exception e) {
            // 否定确认
            channel.basicNack(deliveryTag, false, true);
        }
    }


    @RabbitHandler
    public void handMessage(Message message) throws Exception {
        // 对 Spring AMQP 中的 none 和 auto 消息接收操作类型的测试
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        // 消费者逻辑
        System.out.printf("接收到消息: %s, deliveryTag: %d \n", new String(message.getBody(),"UTF-8"),
                deliveryTag);

        // 进行业务逻辑处理
        System.out.println("业务逻辑处理");
//        int sum = 3/0;
        System.out.println("业务处理完成");
    }
}
